/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package task;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.LinkedList;
import java.util.List;
import javafx.scene.chart.XYChart;

/**
 *
 * @author Dima
 */
public class CSV {
    private List<XYChart.Data<Double, Double>> values;
    private XYChart.Data<Double, Double> firstValueBounds;
    private XYChart.Data<Double, Double> secondValueBounds;

    public CSV(File file) {
        values = new LinkedList<>();
        firstValueBounds = new XYChart.Data<>();
        secondValueBounds = new XYChart.Data<>();
        
        readFile(file);
    }
    
    private void readFile(File file) {
        double minF = Double.MAX_VALUE, maxF = Double.MIN_VALUE, minS = Double.MIN_VALUE, maxS = Double.MIN_VALUE; 
        
        try (BufferedReader br = new BufferedReader(new FileReader(file))) {
            String line;
            while ((line = br.readLine()) != null) {
                String[] tokens = line.split(";");
                
                if (tokens.length != 2) {
                    throw new Exception("Wrong CSV file format");
                }
                
                double val1 = Double.parseDouble(tokens[0]);
                double val2 = Double.parseDouble(tokens[1]);
                
                if (val1 < minF) {
                    minF = val1;
                } else if (val1 > maxF) {
                    maxF = val1;
                }
                
                if (val2 < minS) {
                    minS = val2;
                } else if (val2 > maxS) {
                    maxS = val2;
                }
                
                XYChart.Data<Double, Double> item = new XYChart.Data<>(val1, val2);
                values.add(item);
            }
        } catch (Exception ex) {
            
        }
        
        firstValueBounds.setXValue(minF);
        firstValueBounds.setYValue(maxF);
        secondValueBounds.setXValue(minS);
        secondValueBounds.setYValue(maxS);
    } 
    
    public Double getMinX() {
        return firstValueBounds.getXValue();
    }
    public Double getMaxX() {
        return firstValueBounds.getYValue();
    }
    
    public Double getMinY() {
        return secondValueBounds.getXValue();
    }
    public Double getMaxY() {
        return secondValueBounds.getYValue();
    }

    public List<XYChart.Data<Double, Double>> getValues() {
        return values;
    }
    
    
}
